%ONly does NM+Gurobi

addpath(genpath('/media/sda2/Users/Nermine/Desktop/code/sdp/gurobi301/gurobi_mex_v1.20'));
addpath(genpath('/Users/theja/zResearch_gurobimex/gurobi_mex_v1.20'));
addpath(genpath('./cplexint100'));

clear all;
format short;

global routeCostT2 routeInfo iterate_j C1  indexIteration  modelNumber numUnlabeled C unLabeled;
global numFeatures C0 C2 trainingdata numTrain;

%Step 1: Loading training and validation/test data.
load('../data/trainAndTestFull.mat'); 
varXTest = var(xTest_o);
xTest_o = (xTest_o - repmat(mean(xTest_o),23217,1))./repmat(sqrt(varXTest),23217,1);
varXTrain = var(xTrain_o);
xTrain_o = (xTrain_o - repmat(mean(xTrain_o),23217,1))./repmat(sqrt(varXTrain),23217,1);
numFeatures             = 4;
numTrain                = 23217;
testdata                = [xTest_o yTest_o];        %For validation Only.
trainingdata            = [xTrain_o(1:numTrain,:) yTrain_o(1:numTrain)];
%preprocessing to alleviate the probability values.
% index_train_preprocess1 = find(trainingdata(:,4) > 10);
% numTrain                = length(index_train_preprocess1);
% trainingdata            = trainingdata(index_train_preprocess1,:);

C0 = 1; C1 = 1; C2 = 0.1;   %Default coefficients of each of the terms in OBJ.

%C1arr = [0.005 0.01 0.05 0.1 0.5 1]; %Perfect for 7 node data for Model 1.

%C1arr = [0.005 0.05 0.1 0.2 0.5 1]; %Perfect for 7 node data and Model 2.
%C1arr = [0.5 0.55 0.65 0.75 0.85 0.95 1 1.05 1.15 1.25];
%C1arr = [0.5 0.55 0.85 1.15 1.25]; % the good points from above. choosing
%1.25
C1arr = [0.85];  

for modelNumber=2:2

    for theja2sims = 7:7 %KEEP CHANGING THIS TO LOAD DIFFERENT DATASETS : 2011-02-02
        if      (theja2sims ==6 )
            load ../data/SixNodeData.mat;
        elseif  (theja2sims ==7)
            load ../data/SevenNodeData.mat;
        elseif (theja2sims == 8)
            load ../data/EightNodeData.mat;
        end

        iterate_i=0;iterate_j=0;iterate_k=0;
    
        % % OPTION3: NM+MILP: Via Fminsearch+Gurobi.
        for iterate_j=1:length(C1arr)
            C1 = C1arr(iterate_j);
            tic
            
            %OPTION1: MINLP: via AMPL+BONMIN
            indexIteration = 0;
            opts_Alternating = optimset('display','off','TolFun',1e-6, 'MaxIter', 5000,'MaxFunEvals',10000, 'TolX',1e-6);
            [Lambda_Alternating,fval_Alternating,exitflag,output_Alternating] = fminsearch(@gurobiIterative,zeros(numFeatures+1,1),opts_Alternating);
            
            timeC1_alt(iterate_j) = toc;
            LambdaC1_alt(:,iterate_j) = Lambda_Alternating;
            fvalC1_Alternating(:,iterate_j) = fval_Alternating;
            routeinfoC1{iterate_j} = routeInfo; %{indexIteration};
            routeCostT2C1(iterate_j) = routeCostT2 % unnormalized.
            %save(strcat(['result_alternating_matlab_workspace_Feb5_Alternate_' int2str(modelNumber) '_' int2str(theja2sims) '_' int2str(iterate_j) '.mat']));
            
            display([num2str(iterate_j) '_' num2str(theja2sims) '_' num2str(modelNumber)]);
            display(['Optimization done. IterationIndex is ' num2str(indexIteration)]);
            
        end % End of for loop over C1arr for NM+MILP
    end %choosing between 6/7/8 node data.
    save(strcat(['result_nm_matlab_workspace_Feb7_' int2str(modelNumber) '_' int2str(theja2sims) '_summary.mat']));
end % modelNumber

